openerp.oepetstore = function(instance, local) {
    var _t = instance.web._t, _lt = instance.web._lt;
    var QWeb = instance.web.qweb;

    local.GreetingWidget = instance.Widget.extend({
        className : 'oe_petstore_greetings',
        init : function(parent, name) {
            this._super(parent);
            this.name = name;
        },
        start : function() {
            this.$el.append("<div>We are so happy to see you again in this menu!</div>");
            console.log(this.getParent().$el);
        },
    });

    local.ConfirmWidget = instance.Widget.extend({
        events : {
            'click button.ok_button' : function() {
                this.trigger('user_chose', true);
            },
            'click button.cancel_button' : function() {
                this.trigger('user_chose', false);
            },
        },
        start : function() {
            this.$el.append("<div> Are you sure you want to perform this action?</div>" + "<button class='ok_button'>Ok</button>" + "<button class='cancel_button'>Cancel</button>");
        },
    });

    local.ConfirmWidget.include({
        start : function() {
            console.log("This is inherit");
            return this._super();
        },
    });

    instance.oepetstore.FieldChar2 = instance.web.form.AbstractField.extend({
        init : function() {
            this._super.apply(this, arguments);
            this.set("value", "yes");
        },
        start : function() {
            this.on("change:effective_readonly", this, function() {
                this.display_field();
                this.render_value();
            });
            this.display_field();
            return this._super();
        },
        display_field : function() {
            var self = this;
            this.$el.html(QWeb.render("FieldChar2", {
                widget : this
            }));
            if (! this.get("effective_readonly")) {
                this.$("input").change(function() {
                    self.internal_set_value(self.$("input").val());
                });
            }
        },
        render_value : function() {
            if (this.get("effective_readonly")) {
                this.$el.text(this.get("value"));
            } else {
                this.$("input").val(this.get("value"));
            }
        },
    });

    instance.web.form.widgets.add('char2', 'instance.oepetstore.FieldChar2');

    local.HomePage = instance.Widget.extend({
        template : "HomePageTemplate",
        className : 'oe_petstore_homepage',
        init : function(parent) {
            this._super(parent);
            this.name = "Jeff";
        },
        start : function() {
            var self = this;
            var model = new instance.web.Model("oepetstore.message_of_the_day");
            model.call("my_method", {
                context : new instance.web.CompoundContext()
            }).then(function(result) {
                self.$el.append("<div>Hello " + result["hello"] + "</div>");
            });

            user = new instance.web.Model("res.users");
            user.query(['name', 'login', 'email', 'signature']).filter([['active', '=', true]]).limit(15).all().then(function(users) {
                console.log(users[0]['name']);
                // do something with users
            });
            var greeting = new local.GreetingWidget(this);
            greeting.appendTo(this.$el);
            console.log(this.getChildren()[0].$el);
            var confirm = new local.ConfirmWidget(this);
            confirm.on('user_chose', this, this.user_chose);
            this.widg = new local.ConfirmWidget(this);
            this.widg.on('change:name', this, this.name_changed);
            this.widg.set("name", "Lisa");
            confirm.appendTo(this.$el);
        },
        user_chose : function(confirm) {
            if (confirm) {
                console.log("The user agreed to continue");
            } else {
                console.log("The user refused to continue");
            }
        },
        name_changed : function() {
            //这里的输出不对，取不到name的值
            console.log("The new value of the property 'name' is", this.widg.get("name"));
        }
    });

    instance.web.client_actions.add('petstore.homepage', 'instance.oepetstore.HomePage');
}
